/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package angrypainters.application;

import angrypainters.components.GameModel;
import angrypainters.components.NetworkManager;
import angrypainters.components.ResourceManager;
import angrypainters.components.ScreensManager;
import angrypainters.components.Settings;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.TextField;

/**
 *  Controller class for the settings screen
 *
 * @author A7med
 */
public class SettingsScreenController implements Initializable {

    @FXML
    private TextField addressField;
    private ScreensManager sc;
    private ResourceManager manager;
    private Settings settings=new Settings();
    @FXML
    private Button back;
    GameModel model;
    @FXML
    private TextField opponentIp;
    @FXML
    private ColorPicker picker;
    @FXML
    private TextField name;
    @FXML
    private TextField port;
    @FXML
    private TextField ballons;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        try {
            // set the initial ip address
            addressField.setText(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException ex) {
            Logger.getLogger(SettingsScreenController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }    

   

    public ScreensManager getSc() {
        return sc;
    }

    public void setSc(ScreensManager sc) {
        this.sc = sc;
    }
    /**
     * Displays the start screen
     * @param e 
     */
    public void goBack(ActionEvent e){
        if(model==null){
            model=new GameModel();
            model.setOwnColor(picker.getValue());
            model.setOwnScore(Double.parseDouble(ballons.getText()));
        }
        bindSettings();
        NetworkManager netmManager=new NetworkManager();
        netmManager.setAddress(opponentIp.getText());
        netmManager.setPort(Integer.parseInt(port.getText()));
        manager.setModel(model);
        manager.setOptions(settings);
        manager.setNm(netmManager);
        
        sc.setScreen("start");
        
    }

    public ResourceManager getManager() {
        return manager;
    }

    public void setManager(ResourceManager manager) {
        this.manager = manager;
    }
    /**
     * Transfers the contents of the fields to the settings object
     */
    private void bindSettings(){
        settings.setColor(picker.getValue().toString());
        settings.setDisplayName(name.getText());
        settings.setIp(addressField.getText());
        settings.setOpponentIP(opponentIp.getText());
        settings.setPort(port.getText());
        settings.setBallons(Integer.parseInt(ballons.getText()));
    }
}
